package com.hxht.cmp.module;

import com.github.pagehelper.Page;
import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.*;
import com.hxht.cmp.pojo.FavoritePojo;
import com.hxht.cmp.pojo.FavoriteResult;
import com.hxht.cmp.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gaozikun on 2017/9/4 0004.
 */
@RequestMapping("group")
@Controller
public class GroupModule {
    @Autowired
    private UserService userService;
    @Autowired
    private JudgeService judgeService;
    @Autowired
    private CaseReviewService caseReviewService;
    @Resource
    private FavoriteService favoriteService;
    @Autowired
    private CasePlanService casePlanService;
    @Autowired
    private CaseVideoService caseVideoService;

    @RequestMapping("index")
    private ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        //当前庭长的组员
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        long id = user.getId();
        List<Map<String, Object>> groupUser = userService.selectGroupUser(id);
        modelAndView.addObject("groupUser", groupUser);
        //当前法院的法庭
        List<CourtRoom> courtroomList = new ArrayList<>();
        List<String> courtroomUids = new ArrayList<>();
        for (CourtRoom courtRoom : Constants.COURTROOM_LIST) {
            if (Objects.equals(courtRoom.getCourtUid(), Constants.CENTRAL_COURT.getUid()) && courtRoom.getActive() == 1) {
                courtroomList.add(courtRoom);
                courtroomUids.add(courtRoom.getUid());
            }
        }
        modelAndView.addObject("courtroomList", courtroomList);
        //法官开庭数
        List<String> userUids = new ArrayList<>();
        for (Map<String, Object> map : groupUser) {
            if (null != map.get("userUid")) {
                userUids.add(map.get("userUid").toString());
            }
        }
        modelAndView.addObject("openCourtNum", judgeService.getJudgeCourtStatistical(userUids));
        //法官开庭时长
        modelAndView.addObject("openCourtTime", judgeService.getJudgeCourtTimeStatistical(userUids));
        //法官总容量
        modelAndView.addObject("openCourtSize", judgeService.getJudgeFileSizeStatistical(userUids, null));
        modelAndView.setViewName("group/index");
        return modelAndView;
    }

    @RequestMapping("more_review")
    public ModelAndView moreReview(){
        ModelAndView modelAndView = new ModelAndView();
        //当前庭长的组员
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        long id = user.getId();
        List<Map<String, Object>> groupUser = userService.selectGroupUser(id);
        modelAndView.addObject("groupUser", groupUser);
        //当前法院案件的所有书记员
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(user.getCourtUid());
        modelAndView.addObject("secretaryList",selectJudgeAndSecretary.get("secretaryList"));
        //当前法院的法庭
        List<CourtRoom> courtroomList = new ArrayList<>();
        List<String> courtroomUids = new ArrayList<>();
        for (CourtRoom courtRoom : Constants.COURTROOM_LIST) {
            if (Objects.equals(courtRoom.getCourtUid(), Constants.CENTRAL_COURT.getUid()) && courtRoom.getActive() == 1) {
                courtroomList.add(courtRoom);
                courtroomUids.add(courtRoom.getUid());
            }
        }
        modelAndView.addObject("courtroomList", courtroomList);
        //所有案件类型
        modelAndView.addObject("caseTypeList", caseReviewService.getAllCaseType());
        //收藏
        List<Favorite> favorites = favoriteService.selectAllFavorite(user.getUid());
        List<String> plans = new ArrayList<>();
        for (Favorite favorite:favorites) {
            plans.add(favorite.getPlanUid());
        }
        modelAndView.addObject("favorites", plans);
        modelAndView.setViewName("group/more_review");
        return  modelAndView;
    }

    @RequestMapping("more_courting")
    public ModelAndView moreCourting(){
        ModelAndView modelAndView = new ModelAndView();
        //当前庭长的组员
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        long id = user.getId();
        List<Map<String, Object>> groupUser = userService.selectGroupUser(id);
        modelAndView.addObject("groupUser", groupUser);
        //当前法院案件的所有书记员
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(user.getCourtUid());
        modelAndView.addObject("secretaryList",selectJudgeAndSecretary.get("secretaryList"));
        //当前法院的法庭
        List<CourtRoom> courtroomList = new ArrayList<>();
        List<String> courtroomUids = new ArrayList<>();
        for (CourtRoom courtRoom : Constants.COURTROOM_LIST) {
            if (Objects.equals(courtRoom.getCourtUid(), Constants.CENTRAL_COURT.getUid()) && courtRoom.getActive() == 1) {
                courtroomList.add(courtRoom);
                courtroomUids.add(courtRoom.getUid());
            }
        }
        modelAndView.addObject("courtroomList", courtroomList);
        //所有案件类型
        modelAndView.addObject("caseTypeList", caseReviewService.getAllCaseType());
//        //收藏
//        List<Favorite> favorites = favoriteService.selectAllFavorite(user.getUid());
//        List<String> plans = new ArrayList<>();
//        for (Favorite favorite:favorites) {
//            plans.add(favorite.getPlanUid());
//        }
//        modelAndView.addObject("favorites", plans);
        modelAndView.setViewName("group/more_courting");
        return  modelAndView;
    }

    @RequestMapping("play")
    public ModelAndView play(String planUid,String trialUid) {
        ModelAndView modelAndView = new ModelAndView();
        if (!ObjectUtils.isEmpty(planUid)) {
            if (!ObjectUtils.isEmpty(trialUid)) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trialUid);
                modelAndView.setViewName("group/play");
                return modelAndView;
            }
            List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
            trials.removeIf(tempTrial -> Objects.equals(tempTrial.getStatus(), 1) || Objects.equals(tempTrial.getStatus(), 0));
            if (trials.size() == 1) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trials.get(0).getUid());
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trials.get(0).getUid());
                modelAndView.setViewName("group/play");
                return modelAndView;
            }
            modelAndView.addObject("trialList", trials);
            modelAndView.addObject("planUid", planUid);
        }
        modelAndView.setViewName("group/play_select");
        return modelAndView;
    }

    @RequestMapping("living")
    public ModelAndView living(String planUid, String courtRoomUid, String channel) {
        ModelAndView modelAndView = new ModelAndView();
        List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
        String trialUid = null;
        for (Trial trial : trials)
            if (Objects.equals(trial.getStatus(), 0) || Objects.equals(trial.getStatus(), 1) || Objects.equals(trial.getStatus(), 5)) {
                trialUid = trial.getUid();
            }
        Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
        //将页面中需要的参数携带回去
        modelAndView.addObject("courtRoomUid", courtRoomUid);
        modelAndView.addObject("channel", channel);
        modelAndView.addObject("planUid", planUid);
        modelAndView.addObject("caseMap", caseMap);
        modelAndView.setViewName("group/living");
        return modelAndView;
    }

    @RequestMapping("screenshot")
    public ModelAndView screenshot() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
//        modelAndView.addObject("result", result);
        modelAndView.setViewName("group/screenshot");
        return modelAndView;
    }

    @RequestMapping("favorite")
    public ModelAndView favorite() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        FavoritePojo favoritePojo = new FavoritePojo();
        favoritePojo.setUserUid(user.getUid());
        favoritePojo.setPage(1);
        favoritePojo.setPageSize(12);
        Page<FavoriteResult> favorites = favoriteService.pageFavorite(favoritePojo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<favorites.getResult().size();i++) {
            FavoriteResult favoriteResult = favorites.get(i);
            favoriteResult.setStart(format.format(favoriteResult.getStartDate()));
        }
        modelAndView.addObject("count", favorites.getTotal());
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(null);
        //当前庭长的组员
        long id = user.getId();
        List<Map<String, Object>> groupUser = userService.selectGroupUser(id);
        modelAndView.addObject("judgeList",groupUser);
        modelAndView.addObject("secretaryList",selectJudgeAndSecretary.get("secretaryList"));
        modelAndView.addObject("courts", Constants.COURT_LIST);
        modelAndView.addObject("courtrooms", Constants.COURTROOM_LIST);
        modelAndView.addObject("caseType", caseReviewService.getAllCaseType());
        modelAndView.addObject("result", favorites.getResult());
        modelAndView.setViewName("group/favorite");
        return modelAndView;
    }
}
